package com.cloud.controller;


import com.cloud.aop.DataSource;
import com.cloud.entity.UserEntity;
import com.cloud.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 波波老师(weixin ： javabobo0513)
 * @desc UserController
 */
@RestController
@RequestMapping("/data/all")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
     * 保存数据到ds1数据源中，可以不写，那就是默认选择数据源1
     */
    @GetMapping(value = "/saveDb1")
    @DataSource(name = "db1")
    public void saveDb1() {
        UserEntity user = new UserEntity();
        user.setName("db1");
        user.setAge(11);
        userService.saveUser1(user);
    }

    /**
     * 保存数据到ds2数据源中
     */
    @GetMapping(value = "/saveDb2")
    @DataSource(name = "db2")
    public void saveDb2() {
        UserEntity user = new UserEntity();
        user.setName("db2");
        user.setAge(11);
        userService.saveUser2(user);
    }

    /**
     * 从ds1数据源中查询数据
     */
    @GetMapping(value = "/queryDb1")
    @DataSource(name = "db1")
    public List<UserEntity> queryDb1() {
        return userService.list(null);
    }
}
